package cn.caplike.demo.repository.design.pattern.proxy.cglib.extend;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

/**
 * Description: 测试主类
 *
 * @author LiKe
 * @date 2020-03-14 16:23:46
 */
public class App {

	public static void main(String[] args) {
		 testInterceptor();
//		testFilter();
	}

//	private static void testFilter() {
//		Enhancer e = new Enhancer();
//		// 将被代理类设置成父类
//		e.setSuperclass(Target.class);
//
//		Callback noOp =
//				// 什么操作也不做，代理类直接调用被代理的方法，不进行拦截
//				NoOp.INSTANCE,
//				callback = new TargetInterceptor(), fixedVal = new TargetResultFixed();
//
//		Callback[] callbackArr = new Callback[]{noOp, callback, fixedVal};
//
//		e.setCallbacks(callbackArr);
//		e.setCallbackFilter(new TargetCallbackFilter());
//
//		((Target) e.create()).noOpCall("noOp");
//		System.out.println("-----------------------------------------------------------------------------------------");
//		((Target) e.create()).interceptCall("intercept");
//		System.out.println("-----------------------------------------------------------------------------------------");
//		((Target) e.create()).fixedValueCall("fixedValue");
//	}

	/**
	 * Description: 测试 CGLib 拦截器
	 *
	 * @return void
	 * @author LiKe
	 * @date 2020-03-14 16:54:10
	 */
	private static void testInterceptor() {
		Enhancer e = new Enhancer();
		// 将被代理类设置成父类
		e.setSuperclass(Target.class);
		e.setCallback(new TargetInterceptor());

		((Target) e.create()).interceptCall("intercept");
	}
}
